
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if(isAndroid || isiOS) {
	var touchstart1 = "touchstart";
	var touchmove1 = "touchmove";
	var touchend1 = "touchend";
	var tap1 = "touchstart"
} else {
	var touchstart1 = "mousedown";
	var touchmove1 = "mousemove";
	var touchend1 = "mouseup";
	var tap1 = "click";
}

var media = document.getElementById('bgmusic');
var length;

function autoPlayAudio1() {
	wx.config({
		// 配置信息, 即使不正确也能使用 wx.ready
		debug: false,
		appId: '',
		timestamp: 1,
		nonceStr: '',
		signature: '',
		jsApiList: []
	});
	wx.ready(function() {
		media.play();

	});
}

function fnMusic() {
	//var bgmusic = document.getElementById("bgmusic");
	var music = document.getElementById("music");
	if(media.paused) {
		//bgm.currentTime = length;
		media.play();
	} else {
		media.pause();
	}
}
document.addEventListener("WeixinJSBridgeReady", function() {
	media.play();
}, false);
//$(function() {
//	
//});
var timer1 = null;
var isPage1 = false;

function othermuisc(Murl) {
	media.pause();
	media.src = Murl;
	media.play();
	media.loop = 'loop';
	//tipSound.loop = true;
}

function jishi1() {
	timer1 = requestAnimFrame(jishi1);
	var v_time1 = media.currentTime.toFixed(2);
	if(v_time1 <= 2.04 && v_time1 >= 2.00) {
		media.pause();
		cancelAnimFrame(timer1);
		othermuisc('bgm/bgm-train02.mp3');
		return;
	}
}
var  timePg01_01, timePg01_02, timePg01_03;
(function() {
	var PreLoad = function(a, b) {
		var c = b || {};
		this.source = a, this.count = 0, this.total = a.length, this.onload = c.onload, this.prefix = c.prefix || "", this.init()
	};
	PreLoad.prototype.init = function() {
		var a = this;
		a.source.forEach(function(b) {
			var c = b.replace(/[#\?].*$/, '').substr(b.lastIndexOf(".") + 1).toLowerCase(),
				d = a.prefix + b;
			switch(c) {
				case "js":
					a.script.call(a, d);
					break;
				case "css":
					a.stylesheet.call(a, d);
					break;
				case "mp3":
					a.audio.call(a, d);
					break;
				case "svg":
				case "jpg":
				case "gif":
				case "png":
				case "jpeg":
					a.image.call(a, d)
			}
		})
	}, PreLoad.prototype.getProgress = function() {
		return Math.round(this.count / this.total * 100)
	}, PreLoad.prototype.image = function(a) {
		var b = document.createElement("img");
		this.load(b, a), b.src = a
	}, PreLoad.prototype.stylesheet = function(a) {
		var b = document.createElement("link");
		this.load(b, a), b.rel = "stylesheet", b.type = "text/css", b.href = a, document.head.appendChild(b)
	}, PreLoad.prototype.script = function(a) {
		var b = document.createElement("script");
		this.load(b, a), b.type = "text/javascript", b.src = a, document.head.appendChild(b)
	}, PreLoad.prototype.load = function(a, b) {
		var c = this;
		a.onload = a.onerror = a.onabort = function(a) {
			c.onload && c.onload({
				count: ++c.count,
				total: c.total,
				item: b,
				type: a.type
			})
		}
	};

	var resources = [
		'js/jquery.easing.min.js',
		'js/jquery.min.js',
		'js/jquery.easing.compatibility.min.js',
		'js/jweixin-1.0.0.js',
		'js/jquery.pagepiling.min.js',
		'img/loading.png',
		'img/bg-pg02.png',
		'img/bg-pg02-02-2.png',
		'img/bg-pg02-03.png',
		'img/oldman.png',
		'img/righthand.png',
		'img/bg-pg03-1.png',
		'img/bg-pg03-2.png',
		'img/bg-pg03-3.png',
		'img/bg-pg4-1.png',
		'img/pg03-3-01.jpg',
		'img/pg03-3-02.jpg',
		'img/pg03-3-03.jpg',
		'img/girl-02.png',
		'img/pg02-3-1.png',
	]

	new PreLoad(resources, {
		onload: function(load) {
			var count = load.count,
				total = load.total,

				percent = Math.ceil(100 * count / total) + '',
				htmlStr = '';
			// DOM变化
			document.querySelector('.loading').style.width = (Number(percent) + 20) + '%'
			//						console.log(percent);					
			if(count == total) {
				if(!isPage1) {
					document.querySelector('.loadingout').style.display = "none";
					$('.pg1').removeClass('hide');
					othermuisc('bgm/bgm-train01.mp3');
					autoPlayAudio1();					
					jishi1();
					console.log('load');
//					clearTimeout(timePg01_01);
//					clearTimeout(timePg01_02);
//					clearTimeout(timePg01_03);
					timePg01_01 = setTimeout(function() {
						//console.log('test');
						$('.carriage-text').addClass('opacityfull');
					}, 3000);
					timePg01_02 = setTimeout(function() {
						$('.btn-go').removeClass('hide');
					}, 8000);
					timePg01_03 = setTimeout(function() {
						$('.pg1 .fixed').removeClass('hide');
					}, 9000);
	
				}
			}

		}
	})

	// iPhone4 in WeChat
	var cw = Math.min(document.body.clientWidth, document.body.clientHeight),
		ch = Math.max(document.body.clientWidth, document.body.clientHeight)
	if(ch / cw < 1008 / 640) {

	}
})();

// 翻譯及回調
$(document).ready(function() {
	var music = document.getElementById("music");
	var bgmusic = document.getElementById("bgmusic");
	var firstLoad = true;
	bgmusic.addEventListener("playing", function() {
		music.className = "show play-music";

	});
	bgmusic.addEventListener("pause", function() {
		music.className = "show";
	});

	var timeID_1, timeID_2, timeID_3;
	var timePg02_1, timePg02_2, timePg02_3, timePg03_1, timePg03_2, timePg03_3, timePg03_4, timePg04_1, timePg04_2, timePg04_3, timePg04_4, timePg04_5;
	$('#pagebox').pagepiling({
		sectionsColor: ['#c0dedc', '#c0dedc', '#c0dedc', '#c0dedc', '#c0dedc'],
		anchors: ['page1', 'page2', 'page3', 'page4', 'page5'],
		menu: false,
		easing: 'backinout',
		loopBottom: true,
		css3: true,
		animateAnchor: true,
		setAllowScrolling: true,
		afterRender: function(anchorLink, index) {
			//$.fn.pagepiling.moveTo(1);

		},
		afterLoad: function(anchorLink, index) {
			if(index == 1) {
				media.play();
				autoPlayAudio1();
				if(firstLoad) {
					media.play();
					autoPlayAudio1();
					jishi1();
				} else {
					
					othermuisc('bgm/bgm-train01.mp3');
					jishi1();
				}
				console.log('index1');
				$(".bg-pg02-1").removeClass("hide").siblings().addClass("hide");
				$('.pg1').removeClass('hide');
				clearTimeout(timePg01_01);
				clearTimeout(timePg01_02);
				clearTimeout(timePg01_03);
				timePg01_01 = setTimeout(function() {
					//console.log('test');
					$('.carriage-text').addClass('opacityfull');
				}, 3000);
				timePg01_02 = setTimeout(function() {
					$('.btn-go').removeClass('hide');
				}, 8000);
				timePg01_03 = setTimeout(function() {
					$('.pg1 .fixed').removeClass('hide');
				}, 9000);
			}
			if(index == 2) {
				clearTimeout(timePg01_01);
				clearTimeout(timePg01_02);
				clearTimeout(timePg01_03);
				firstLoad = false;
				othermuisc('bgm/bgm-main.mp3');
				$(".bg-pg3-1").removeClass("hide").siblings().addClass("hide");
				$('.textbox-boy-01').removeClass('hide');
				clearTimeout(timePg02_1);
				timePg02_1 = setTimeout(function() {
					$("#btn-call-boy").removeClass("hide");
				}, 3000);

				$('#btn-call-boy').click(function() {
					clearTimeout(timeID_1);
					$(".bg-pg02-1").addClass("hide");
					$(".bg-pg02-2").removeClass("hide");
					timeID_1 = setTimeout(function() {
						$('#textbox-boy-02').css('animation', 'button-anim 2s 0s infinite both');
					}, 2000);

				})

				$('#textbox-boy-02').click(function() {
					//othermuisc('bgm/music.mp3');
					$(".bg-pg02-2").addClass("hide");
					$(".bg-pg02-3").removeClass("hide");
					clearTimeout(timePg02_2);
					clearTimeout(timePg02_3);
					othermuisc('bgm/bgm-Transition.mp3');
					timePg02_2 = setTimeout(function() {
						$('.pg2-3-kuang,.textbox-boy-03').removeClass('hide');
						$('.pg2-3-kuang,.textbox-boy-03').css('animation', 'Shaking-anim 1s 0s infinite both')
						$(".Arrow").removeClass("hide");
					}, 1000);
					timePg02_3 = setTimeout(function() {
						othermuisc('bgm/bgm-main.mp3');
					}, 2800);
				})
			}
			if(index == 3) {
				$(".bg-pg02-1").removeClass("hide").siblings().addClass("hide");
				$(".bg-pg4-1").removeClass("hide").siblings().addClass("hide");
				$('#btn-call-girl').click(function() {
					clearTimeout(timeID_2);
					clearTimeout(timeID_3);
					$(".bg-pg3-1").addClass("hide");
					$(".bg-pg3-2").removeClass("hide");
					timeID_2 = setTimeout(function() {
						$('#btn-call-girl02').removeClass("hide");
					}, 3000);
					timeID_3 = setTimeout(function() {
						$('#btn-call-girl02').css('animation', 'button-anim 2s 0.5s infinite both');
					}, 3800);
				})
				$('#btn-call-girl02').click(function() {
					othermuisc('bgm/bgm-Transition.mp3');
					clearTimeout(timePg03_1);
					clearTimeout(timePg03_2);
					clearTimeout(timePg03_3);
					clearTimeout(timePg03_4);
					$(".bg-pg3-2").addClass("hide");
					$(".bg-pg3-3").removeClass("hide");
					timePg03_3 = setTimeout(function() {
						$('.girl-Back').removeClass('hide');
					}, 2000);
					timePg03_4 = setTimeout(function() {
						othermuisc('bgm/bgm-main.mp3');
					}, 2800);
					timePg03_1 = setTimeout(function() {
						$('.pg3 .pg2-3-kuang ,.pg3 .textbox-boy-03').removeClass('hide');
					}, 5000);
					timePg03_2 = setTimeout(function() {
						$('.pg3 .pg2-3-kuang ,.pg3 .textbox-boy-03').css('animation', 'Shaking-anim 1s 0s infinite both');
						$(".Arrow").removeClass("hide");
					}, 6000);
				})
			}
			if(index == 4) {
				$(".bg-pg3-1").removeClass("hide").siblings().addClass("hide");
				$('#btn-call-old').click(function() {
					$('.bg-pg4-1').addClass('hide');
					$('.bg-pg4-2').removeClass('hide'); //show old-page2
					clearTimeout(timePg04_1);
					clearTimeout(timePg04_2);

					timePg04_1 = setTimeout(function() {
						$('.oldwoman-text').removeClass('hide');
					}, 2000);
					timePg04_2 = setTimeout(function() {
						$('.oldwoman-text').css('animation', 'button-anim 2s 0.5s infinite both');
					}, 3000);

				})
				$('#oldwoman-text').click(function() {
					clearTimeout(timePg04_3);
					clearTimeout(timePg04_4);
					clearTimeout(timePg04_5);
					othermuisc('bgm/bgm-Transition.mp3');
					$('.bg-pg4-2').addClass('hide');
					$('.bg-pg4-3').removeClass('hide');
					timePg04_3 = setTimeout(function() {
						$('.pg4 .pg2-3-kuang ,.pg4 .textbox-boy-03').removeClass('hide');
					}, 2000);
					timePg04_5 = setTimeout(function() {
						othermuisc('bgm/bgm-main.mp3');
					}, 2800);
					timePg04_4 = setTimeout(function() {
						$('.pg4 .pg2-3-kuang ,.pg4 .textbox-boy-03').css('animation', 'Shaking-anim 1s 0s infinite both');
						$(".Arrow").removeClass("hide");
					}, 3000);
				})

			}
			if(index == 5) {
				$(".bg-pg4-1").removeClass("hide").siblings().addClass("hide");
			}
		},
		onLeave: function(index, direction) {
			if(index == 1) {
				$('.carriage-text').removeClass('opacityfull');
				$('.btn-go,.pg1,.pg1 .fixed').addClass('hide');
				console.log('test');
				clearTimeout(timePg01_01);
				clearTimeout(timePg01_02);
				clearTimeout(timePg01_03);
			}
			if(index == 2) {
				$(".Arrow").addClass("hide");
				$('.textbox-boy-01,#btn-call-boy,.pg2-3-kuang,.textbox-boy-03').addClass('hide');
				$('.pg2-3-kuang,.textbox-boy-03,#textbox-boy-02').css('animation', '')
				clearTimeout(timeID_1);
				clearTimeout(timePg02_1);
				clearTimeout(timePg02_2);
				clearTimeout(timePg02_3);
			}
			if(index == 3) {
				$(".Arrow").addClass("hide");
				$('#btn-call-girl02,.pg3 .pg2-3-kuang ,.pg3 .textbox-boy-03').addClass("hide");
				$('#btn-call-girl02,.pg3 .pg2-3-kuang ,.pg3 .textbox-boy-03').css('animation', '');
				clearTimeout(timeID_2);
				clearTimeout(timeID_3);
				clearTimeout(timePg03_1);
				clearTimeout(timePg03_2);
				clearTimeout(timePg03_3);
				clearTimeout(timePg03_4);
			}
			if(index == 4) {
				$(".Arrow").addClass("hide");
				$('.pg4 .pg2-3-kuang ,.pg4 .textbox-boy-03').addClass("hide");
				$('.pg4 .pg2-3-kuang ,.pg4 .textbox-boy-03,.oldwoman-text').css('animation', '');
				clearTimeout(timePg04_1);
				clearTimeout(timePg04_2);
				clearTimeout(timePg04_3);
				clearTimeout(timePg04_4);
				clearTimeout(timePg04_5);
			}
		},

	});

});